/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("PatientReCord",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VoL.Domain.IServices;
using VOL.Core.Utilities;
using Qiniu.Storage;
using Qiniu.Util;

namespace VoL.Domain.Controllers
{
    public partial class PatientReCordController
    {

        // 首先定义一个返回类型
        public class QiniuTokenResponse
        {
            public string Token { get; set; }
            public string Domain { get; set; }
        }


        private const string ACCESS_KEY = "aGIE9q1m-fLuTSbyezfsFt5t9zooaL16kQyHfhcr";
        private const string SECRET_KEY = "2I3967l_5Bf6c96kTsNlaks9WWbvontbdZxz6jn3";
        private const string BUCKET = "zhangruonan123";
        private const string DOMAIN = "http://swnwjy76h.hb-bkt.clouddn.com"; // 你的七牛云域名


        private readonly IPatientReCordService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;


        [ActivatorUtilitiesConstructor]
        public PatientReCordController(
            IPatientReCordService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 新增护理记录单
        /// </summary>
        /// <param name="patientReCord"></param>
        /// <returns></returns>
        [HttpPost("CreatePatientReCord")]
        public async Task<WebResponseContent> CreatePatientReCord([FromBody] PatientReCord patientReCord)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            int i = await _service.CreatePatientReCord(patientReCord);
            return webResponseContent.OK("新增护理记录单成功", i);
        }

        /// <summary>
        /// 上传七牛云
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetQiniuToken")]
        public async Task<IActionResult> GetQiniuToken()
        {
            try
            {
                Mac mac = new Mac(ACCESS_KEY, SECRET_KEY);
                PutPolicy putPolicy = new PutPolicy
                {
                    Scope = BUCKET,
                    DeleteAfterDays = 100
                };
                putPolicy.SetExpires(3600);

                string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

                var response = new QiniuTokenResponse
                {
                    Token = token,
                    Domain = DOMAIN
                };

                return Ok(new { success = true, data = response });
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = "获取上传凭证失败：" + ex.Message });
            }
        }
    }
}
